package com.example.mypra1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.clj.memoryspinner.MemorySpinner;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 爱情测试
 */
public class LoveActivity extends AppCompatActivity {
    private TextView pd1,pd2,pd5,pd6,pd7;//配对指数，配对比重，结果评述，恋爱建议，注意事项文本显示控件
    private SimpleRatingBar pd3,pd4;//两情相悦指数和天长地久指数星星控件
    private MemorySpinner girl,boy;//双方星座选择下拉框
    private String boy_xz,girl_xz;//双方星座
    private Handler handler=new Handler(){//操作UI

        @Override
        public void handleMessage(@NonNull Message msg) {
            Map<String,Object> map = (Map<String, Object>) msg.obj;
            pd1.setText(map.get( "zhishu").toString());
            pd2.setText(map.get( "bizhong").toString());
            pd5.setText(map.get( "jieguo").toString());
            pd6.setText(map.get( "lianai").toString());
            pd7.setText(map.get( "zhuyi").toString());
            pd3.setRating(Float.valueOf(map.get("xiangyue").toString())/20);
            pd4.setRating(Float.valueOf(map.get("tcdj").toString())/20);


        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love);
        //获取UI控件
        pd1 = findViewById(R.id.pd1);
        pd2 = findViewById(R.id.pd2);
        pd3 = findViewById(R.id.pd3);
        pd4 = findViewById(R.id.pd4);
        pd5 = findViewById(R.id.pd5);
        pd6 = findViewById(R.id.pd6);
        pd7 = findViewById(R.id.pd7);
        girl = findViewById(R.id.girl);
        boy = findViewById(R.id.boy);
        boy_xz = "白羊";//默认参数
        girl_xz = "白羊";
        ArrayList<String> list = new ArrayList<>(Arrays.asList("白羊","金牛","双子","巨蟹","狮子","处女",
                "天枰","天蝎","射手","摩羯","水瓶","双鱼"));
       girl.setData(null,list);
        girl.setSelection(0);
       girl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               girl_xz = list.get(i);
               getData();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        boy.setData(null,list);
        boy.setSelection(0);
        boy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                boy_xz = list.get(i);
                getData();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        getData();
    }
    //发送请求获取配对结果数据
    private void getData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://apis.juhe.cn/xzpd/query?key=69bb9d2b3c079c3e3ae347d6b6426005&men="+boy_xz+"&women="+girl_xz)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            Log.d("resp",response.toString());
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                Log.d("res",res);
                                try {
                                    JSONObject jsonObject1 = new JSONObject(res);
                                    JSONObject jsonObject = jsonObject1.getJSONObject("result");
                                    Map<String,String>map = new HashMap<>();
                                    map.put("zhishu",jsonObject.getString("zhishu"));
                                    map.put("bizhong",jsonObject.getString("bizhong"));
                                    map.put("jieguo",jsonObject.getString("jieguo"));
                                    map.put("xiangyue",jsonObject.getString("xiangyue"));
                                    map.put("tcdj",jsonObject.getString("tcdj"));
                                    map.put("lianai",jsonObject.getString("lianai"));
                                    map.put("zhuyi",jsonObject.getString("zhuyi"));
                                    Message message = new Message();
                                    message.obj = map;
                                    handler.sendMessage(message);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                Log.d("resp",response.toString());
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}